﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Sector7.Framework.DB;
using Sector7.Medical.DAO.Standard;
using Sector7.Medical.EntityType;
using Sector7.Framework.Core;
using Sector7.Framework.Utility;
using Sector7.Framework.Enums;
using Sector7.Framework.DAO.Standard;
using Sector7.Medical.Consts;
using System.IO;
using Sector7.Medical.Enums;
using Sector7.Framework.Consts;

namespace Sector7.Medical.Services
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码和配置文件中的类名“SystemService”。
    public class SystemService : ISystemService
    {
        public int GetUnarySeed(string arg1)
        {
            return DAOFactory.GetDAO<SeedDAO>().ReadUnarySeed(arg1);
        }

        public int GetBinarySeed(string arg1, string arg2)
        {
            return DAOFactory.GetDAO<SeedDAO>().ReadBinarySeed(arg1, arg2);
        }

        public int GetTernarySeed(string arg1, string arg2, string arg3)
        {
            return DAOFactory.GetDAO<SeedDAO>().ReadTernarySeed(arg1, arg2, arg3);
        }

        public string GetUsername(string fullName)
        {
            string username = null;

            if (fullName.Length > 1)
            {
                // 1. 分别获取姓名字符串
                string lastName = CharacterUtil.ConvertToSpellString(fullName.Substring(0, 1), CharCaseType.Lower);
                string firstName = CharacterUtil.ConvertToSpellString(fullName.Substring(1), CharCaseType.Lower);

                // 2. 转换姓为5位拼音字符串
                int lastNameLength = lastName.Length;
                if (lastNameLength >= 5)
                {
                    lastName = lastName.Substring(0, 5);
                }
                else
                {
                    lastName += new string(lastName[lastNameLength - 1], 5 - lastNameLength);
                }

                // 3. 转换名为3位拼音字符串
                int firstNameLength = firstName.Length;
                if (firstNameLength >= 3)
                {
                    firstName = firstName.Substring(0, 3);
                }
                else
                {
                    firstName += new string(firstName[firstNameLength - 1], 3 - firstNameLength);
                }

                // 4. 组合用户名
                username = lastName + firstName;

                // 5. 判断用户名是否重复
                int count = DAOFactory.GetDAO<CitizenDAO>().Count(Sector7.Medical.Consts.C_ColumnName.USERNAME, username);                
                if (count > 0)
                {
                    int seed = DAOFactory.GetDAO<SeedDAO>().ReadUnarySeed(username);
                    username = string.Format("{0}{1}{2}",
                        lastName,
                        seed,
                        firstName);
                }
            }

            return username;
        }

        public string Login(string username, string password, AccessRoleType accessRole)
        {
            string result = string.Empty;
            int errorCode = C_ErrorCode.ERR_ACC_SUCCESS;

            switch (accessRole)
            {
                case AccessRoleType.Employee:
                    result = DAOFactory.GetDAO<EmployeeDAO>().GetUniqueId(username, password);
                    if (result.Equals(string.Empty))
                    {
                        errorCode = C_ErrorCode.ERR_ACC_NOT_MATCH;
                    }
                    break;
                default:
                    errorCode = C_ErrorCode.ERR_ACC_UNKNOWN_ROLE;
                    break;
            }

            if (errorCode != C_ErrorCode.ERR_ACC_SUCCESS)
            {
                result = string.Format("{0}#{1}",
                    errorCode,
                    C_ErrorCode.ToString(errorCode));
            }

            return result;
        }
    }
}
